package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.ams.domain.vo.*;
import com.ruoyi.ams.service.IAmsAssetTemplateService;
import com.ruoyi.ams.service.IAmsAssetTypeService;

import com.ruoyi.asms.service.IAsmsAssetFullInfoService;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.AmsAssetTemplate;
import com.ruoyi.common.core.domain.model.AmsAssetType;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 资产型号信息Controller
 *
 * @author yfkj
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/ams/AmsAssetTemplate")
public class AmsAssetTemplateController extends BaseController {
    @Autowired
    private IAmsAssetTemplateService amsAssetTemplateService;

    @Autowired
    private IAmsAssetTypeService amsAssetTypeService;

    @Autowired
    private IAsmsAssetFullInfoService assetFullInfoService;

    /**
     * 查询资产型号信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetTemplate:list")
    @GetMapping("/list")
    @ApiOperation("查询资产型号信息列表")
    public TableDataInfo list(AssetTemplateListVo vo) {
//        if (vo.getAssetTypeId() == null) {
//            return getDataTable(new ArrayList<>());
//        }
        AmsAssetTemplate amsAssetTemplate = new AmsAssetTemplate();
        BeanUtils.copyProperties(vo, amsAssetTemplate);
        startPage();
        List<AmsAssetTemplate> list = amsAssetTemplateService.selectAmsAssetTemplateList(amsAssetTemplate);
        return getDataTable(list);
    }

    /**
     * 根据分类ID获取分类型号列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetTemplate:list")
    @GetMapping("/typeTemplateList")
    @ApiImplicitParams({@ApiImplicitParam(name = "typeId", value = "分类Id")})
    @ApiOperation(value = "根据分类ID获取分类型号列表", response = AmsAssetTemplate.class)
    public TableDataInfo list(@RequestParam(value = "typeId") Long typeId) {
        startPage();
        List<AmsAssetTemplate> list = amsAssetTemplateService.list(new QueryWrapper<AmsAssetTemplate>().eq(AmsAssetTemplate.ASSET_TYPE_ID, typeId));
        return getDataTable(list);
    }

    /**
     * 导出资产型号信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetTemplate:export")
    @Log(title = "资产型号信息", businessType = BusinessType.EXPORT)
    @PostMapping(value="/export", produces = "application/octet-stream")
    @ApiOperation("导出资产型号信息列表")
    public void export(HttpServletResponse response, AmsAssetTemplate amsAssetTemplate) throws IOException {
        List<AmsAssetTemplate> list = amsAssetTemplateService.selectAmsAssetTemplateList(amsAssetTemplate);
        ExcelUtil<AmsAssetTemplate> util = new ExcelUtil<AmsAssetTemplate>(AmsAssetTemplate.class);
        util.exportExcel(response, list, "资产型号信息数据");
    }

    /**
     * 获取资产型号信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetTemplate:query")
    @GetMapping(value = "/{assetTemplateId}")
    @ApiOperation("获取资产型号信息详细信息,assetTemplateId-资产型号信息ID-Long-必填")
    public AjaxResult getInfo(@PathVariable("assetTemplateId") Long assetTemplateId) {
        AmsAssetTemplate amsAssetTemplate = amsAssetTemplateService.selectAmsAssetTemplateByAssetTemplateId(assetTemplateId);
        AmsAssetType assetType = amsAssetTypeService.getOne(new QueryWrapper<AmsAssetType>()
                .eq(AmsAssetType.ASSET_TYPE_ID, amsAssetTemplate.getAssetTypeId()));
        AssetTemplateInfoVo templateInfoVo = new AssetTemplateInfoVo();
        BeanUtils.copyProperties(amsAssetTemplate, templateInfoVo);
        templateInfoVo.setAssetTypeName(assetType.getAssetTypeName());
        return AjaxResult.success(templateInfoVo);
    }

    /**
     * 新增资产型号信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetTemplate:add")
    @Log(title = "资产型号信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增资产型号信息")
    public AjaxResult add(@RequestBody AssetTemplateAddVo vo) {
        AmsAssetTemplate amsAssetTemplate = new AmsAssetTemplate();
        BeanUtils.copyProperties(vo, amsAssetTemplate);
        AmsAssetTemplate template = amsAssetTemplateService.getOne(new QueryWrapper<AmsAssetTemplate>()
                .eq(AmsAssetTemplate.ASSET_TEMPLATE_NAME, vo.getAssetTemplateName()), false);
        if (template != null) {
            return AjaxResult.error(vo.getAssetTemplateName() + "已存在！");
        }
        amsAssetTemplate.setCreateBy(SecurityUtils.getUsername());
        amsAssetTemplate.setCreateTime(System.currentTimeMillis());
        return toAjax(amsAssetTemplateService.insertAmsAssetTemplate(amsAssetTemplate));
    }

    /**
     * 修改资产型号信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetTemplate:edit")
    @Log(title = "资产型号信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改资产型号信息")
    public AjaxResult edit(@RequestBody AssetTemplateEditVo vo) {
        AmsAssetTemplate amsAssetTemplate = new AmsAssetTemplate();
        BeanUtils.copyProperties(vo, amsAssetTemplate);
        amsAssetTemplate.setUpdateBy(SecurityUtils.getUsername());
        amsAssetTemplate.setUpdateTime(System.currentTimeMillis());

        //修改资产视图表中的相关数据
        assetFullInfoService.update(new UpdateWrapper<AsmsAssetFullInfo>()
                .set(AsmsAssetFullInfo.ASSET_TEMPLATE_NAME,vo.getAssetTemplateName())
                .set(AsmsAssetFullInfo.SPECIFICATION,vo.getSpecification())
                .set(AsmsAssetFullInfo.MEASURE_UNIT,vo.getMeasureUnit())
                .eq(AsmsAssetFullInfo.ASSET_TEMPLATE_ID,vo.getAssetTemplateId()));
        return toAjax(amsAssetTemplateService.updateAmsAssetTemplate(amsAssetTemplate));
    }

    /**
     * 删除资产型号信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetTemplate:remove")
    @Log(title = "资产型号信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{assetTemplateId}")
    @ApiOperation("删除资产型号信息,assetTemplateId-资产型号信息ID-Long-必填")
    public AjaxResult remove(@PathVariable Long assetTemplateId) {
        return toAjax(amsAssetTemplateService.deleteAmsAssetTemplateByAssetTemplateId(assetTemplateId));
    }

    @GetMapping("/getListByTypeId")
    public R<List<AmsAssetTemplate>> getListByTypeId(@RequestParam("typeId") Long typeId) {
        return R.ok(amsAssetTemplateService.list(new QueryWrapper<AmsAssetTemplate>().eq(AmsAssetTemplate.ASSET_TYPE_ID, typeId)));
    }

    @PostMapping("/importTypeData")
    public AjaxResult importTypeData(MultipartFile file) throws Exception {
        ExcelUtil<ImportTypeVo> excelUtil = new ExcelUtil<>(ImportTypeVo.class);
        List<ImportTypeVo> list = excelUtil.importExcel(file.getInputStream());
        List<AmsAssetTemplate> templates = new ArrayList<>();
        for (ImportTypeVo vo : list) {
            AmsAssetType type = amsAssetTypeService.getOne(new QueryWrapper<AmsAssetType>()
                    .eq(AmsAssetType.ASSET_TYPE_NAME, vo.getNewName()), false);
            AmsAssetTemplate template = new AmsAssetTemplate();
            template.setAssetTemplateName(vo.getName());
            template.setAssetTypeId(type.getAssetTypeId());
            template.setCreateBy(SecurityUtils.getUsername());
            template.setCreateTime(System.currentTimeMillis());
            templates.add(template);
        }
        amsAssetTemplateService.saveBatch(templates);
        return AjaxResult.success("导入成功！");
    }
}
